package com.wst.controller;


import com.wst.http.HttpResult;
import com.wst.service.weather.WeatherService;
import com.wst.util.Result;
import com.wst.util.ResultUtil;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WeatherController
 * @Description TODO
 * @Author yanyi
 * @Date 2019/6/12 16:11
 * @Version 1.0
 **/
@Controller
@RequestMapping("/weather")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private CloseableHttpClient httpClient;

    @Autowired
    private RequestConfig config;


    /**
     * @Author yanyi
     * @Description //TODO 根据设备id获取设备数据
     * @Date 17:25 2019/6/13
     * @Param [id]
     * @return com.wst.util.Result<java.lang.Object>
     **/
    @ResponseBody
    @RequestMapping(value = "/getWeatherById", method = RequestMethod.POST)
    public Result<Object> getWeatherById(String id){
        HttpResult httpResult=weatherService.getWeatherById(id,"http://iot.whxph.com:90/intfa/queryData/");
        if(httpResult.getCode()==200){
           return  ResultUtil.success(httpResult.getBody());
        }
        return ResultUtil.error(500,"错了");
    }


    @ResponseBody
    @RequestMapping(value = "/getWeather", method = RequestMethod.POST)
    public Map<String,Object> getWeather(){
        Map<String,Object> map=new HashMap<String,Object>();
        // 声明 http get 请求
        HttpGet httpGet = new HttpGet("http://iot.whxph.com:90/intfa/queryData/18000023.json");
        // 装载配置信息
        httpGet.setConfig(config);
        CloseableHttpResponse response=null;
        // 发起请求
        try {
            response = this.httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 判断状态码是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
            // 返回响应体的内容
            try {
                String data=EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(data);
                map.put("data","1231");
                //return ResultUtil.success(data);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return map;
        //return ResultUtil.error(500,"错误了");
    }




}
